<?php
class Tasks_model extends DbManage_model
{

	function __construct()
	{
		$this->load->model('table/taskstable_model','taskstable');
		parent::__construct();
	}

	/**
	 * update task form data
	 *
	 * @param array $arrData,the array including task info and tag info,for example 
	 * 				$arrData=array(
	 * 				'tasks'=>array('id'=>'2','userId'=>'x','type'=>'x','task'=>'x',
	 * 				'originalTextId'=>'x','status'=>'x','verifyStatus'=>'x','agentId'=>'x'),
	 * 				'tasks_tags'=>array(0=>array('taskId'=>'x','tagId'=>'x','tagName'=>'x'),
	 * 				1=>array('taskId'=>'x','tagId'=>'x','tagName'=>'x')),
	 * 				);
	 * @return return true or false
	 */
	function updateFormData($arrData)
	{
		try
		{
			$this->load->model('table/taskstagstable_model','taskstagstable');
			$intTaskId=$arrData[$this->taskstable->strTable][$this->taskstable->strPrimaryKey];
			$this->db->trans_begin();
			$this->taskstable->update($arrData[$this->taskstable->strTable],array($this->taskstable->strPrimaryKey=>$intTaskId));
			$this->taskstagstable->updateTagsOfTask($arrData[$this->taskstagstable->strTable],$intTaskId);
			$this->db->trans_commit();
		}
		catch (Exception $e)
		{
			$this->db->trans_rollback();
			$strMessage=sprintf("%s:%d %s", $e->getFile (), $e->getLine (), $e->getMessage ());
			log_message('error',$strMessage);
			return false;
		}
		return true;
	}

	/**
	 * get task form data
	 *
	 * @param int $intId the id of task
	 * @return return an array including data if success ,return false if failed
	 * 				for example
	 * 				$arrData=array(
	 * 				'tasks'=>array('id'=>'x','userId'=>'x','type'=>'x','task'=>'x',
	 * 				'originalTextId'=>'x','status'=>'x','verifyStatus'=>'x','agentId'=>'x'),
	 * 				'tasks_tags'=>array(0=>array('id'=>'x','taskId'=>'x','tagId'=>'x','tagName'=>'x'),
	 * 				1=>array('id'=>'x','taskId'=>'x','tagId'=>'x','tagName'=>'x')),
	 * 				);
	 * 
	 */
	function getFormData($intId)
	{
		try
		{
			$this->load->model('table/taskstagstable_model','taskstagstable');
			$this->load->model('table/originaltexttable_model','originaltexttable');
			$arrResult=array();
			$arrResult[$this->taskstable->strTable]=current($this->taskstable->select(array($this->taskstable->strPrimaryKey=>$intId)));
			$arrResult[$this->taskstagstable->strTable]=$this->taskstagstable->select(array($this->taskstagstable->strForeignKey=>$intId));
			$arrResult[$this->originaltexttable->strTable]=current($this->originaltexttable->select(
			array($this->originaltexttable->strPrimaryKey=>$arrResult[$this->taskstable->strTable]['originalTextId'])));
			return $arrResult;
		}
		catch (Exception $e)
		{
			$strMessage=sprintf("%s:%d %s", $e->getFile (), $e->getLine (), $e->getMessage ());
			log_message('error',$strMessage);
			return false;
		}
	}

//	function normalSearch($arrWhere)
//	{
//		try
//		{
//			$arrData=$this->taskstable->select($arrWhere);
//			return $arrData;
//		}
//		catch (Exception $e)
//		{
//			$strMessage=sprintf("%s:%d %s", $e->getFile (), $e->getLine (), $e->getMessage ());
//			log_message('error',$strMessage);
//		}
//	}


	/**
	 * get task list data
	 *
	 * @param array $arrWhere the conditions ,for example $arrWhere=array('userId'=>'3');
	 * @param int $intLimit
	 * @return return an array including data if success ,return false if failed
	 * 				for example
	 * 				$arrData=>array(
	 * 				0=>array('id'=>'x','userId'=>'x','type'=>'x','task'=>'x',
	 * 				'originalTextId'=>'x','status'=>'x','verifyStatus'=>'x','agentId'=>'x'),
	 * 				1=>array('id'=>'x','userId'=>'x','type'=>'x','task'=>'x',
	 * 				'originalTextId'=>'x','status'=>'x','verifyStatus'=>'x','agentId'=>'x'),
	 * 				);
	 */
	function listSearch($arrWhere=array(),$intLimit=0)
	{
		try
		{
			$arrData=$this->taskstable->select($arrWhere,$intLimit);
			return $arrData;
		}
		catch (Exception $e)
		{
			$strMessage=sprintf("%s:%d %s", $e->getFile (), $e->getLine (), $e->getMessage ());
			log_message('error',$strMessage);
			return false;
		}
	}


}
